<?php
/**
* Django's templating engine (partial) PHP implementation
* @link http://djangoproject.com Django
* this file is from a webappkit package (kit)
* @link http://webappkit.net
* @package djtpl
* @author J.Ducastel <jeremie@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
* template token
* @author J.Ducastel <jeremie@ducastel.name>
*/
class djtplToken {
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
	/**
	* constructor
	* @access public
	*/
	function djtplToken(&$str) {
		$this->__construct($str);
	}/**/
	
	/**
	* real constructor
	* @access public
	*/
	function __construct(&$str) {
		$this->str=&$str;
		if (substr($str,0,2)=='{{') {
			$this->setAsVar();
		} else if (substr($str,0,2)=='{%') {
			$this->setAsTag();
		} else {
			$this->type='text';
		}
	}/**/

	/**
	* get token type.
	* @access public
	* @return string text|tag|var
	*/
	function getType() {
		return $this->type;
	}/**/
	
	/**
	* is the token a tag token ?
	* @access public
	* @return bool
	*/
	function isTag() {
		return $this->type=='tag';
	}/**/
	
	/**
	* is the token a var token ?
	* @access public
	* @return bool
	*/
	function isVar() {
		return $this->type=='var';
	}/**/
	
	/**
	* is the token a text token ?
	* @access public
	* @return bool
	*/
	function isText() {
		return $this->type=='text';
	}/**/
	
	/**
	* return tag name if relevant
	* @access public
	* @return false|string
	*/
	function getTagName() {
		if ($this->getType()!='tag')
			return false;
		return $this->tagname;
	}/**/
	
	/**
	* return tag definition if relevant
	* @access public
	* @return string
	*/
	function getTagDef() {
		if ($this->getType()!='tag')
			return false;
		return $this->tagdef;
	}/**/
	
	/**
	* return var name if relevant
	* @access public
	* @return string
	*/
	function getVarName() {
		if ($this->getType()!='var')
			return false;
		return $this->varname;
	}/**/
	
	/**
	* return var filters if relevant
	* @access public
	* @return array
	*/
	function getVarFilters() {
		if ($this->getType()!='var')
			return false;
		return $this->filters;
	}/**/

	/**
	* @var string text|var|tag|invalid
	*/
	var $type='invalid'; 
	/**
	* @var string content
	*/
	var $str='';
	//var $tagname;
	//var $tagdef;
	//var 

	/**
	* sets as a tag token
	* @access private
	* @return bool
	*/
	function setAsTag() {
		if (!ereg('^\{% ?([a-zA-Z]+) ?(.*) ?%\}$',$this->str,$search)) {
			$this->type='invalid';
			return false;
		}
		$this->tagname=trim($search[1]);
		$this->tagdef=trim($search[2]);
		$this->type='tag';
		return true;
	}/**/
	
	/**
	* parse token string and sets token as var
	* @access private
	* @return bool
	*/
	function setAsVar() {
		// extracting definition from 
		if (!ereg('^\{\{ ?([^{}]+) ?\}\}$',$this->str,$search)) {
			$this->type='invalid';
			return false;
		}
		$def=trim($search[1]);
		$parts=explode('|',$def);
		// first part is var name
		$this->varname=array_shift($parts);
		// all other parts are filters
		$this->filters=$parts;
		$this->type='var';
		return true;
	}/**/
}/* end of djtplToken class */
?>